class Solution {
public:
    int maxSumDivThree(vector<int>& nums) {
        const int MAX = 0x3f3f3f3f;

        int sum = 0, x1 = MAX, x2 = MAX, y1 = MAX, y2 = MAX;
        for (auto e : nums)
        {
            sum += e;
            if (e % 3 == 1)
            {
                if (e < x1)  x2 = x1, x1 = e;
                else if (e < x2) x2 = e;
            }
            if (e % 3 == 2)
            {
                if (e < y1) y2 = y1, y1 = e;
                else if (e < y2) y2 = e;
            }
        }
        if (sum % 3 == 0) return sum;
        else if (sum % 3 == 1) return max(sum - x1, sum - y1 - y2);
        else return max(sum - y1, sum - x1 - x2);
    }
};